home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 123 / MacAddict_123_2006_11.iso / Software / Productivity / iClip lite 2.wdgt / js / button.js < prev    next >
Text File  |  2006-06-16  |  2KB  |  102 lines

  1. // Copyright ¬© 2006, Inventive. //
  2.  
  3. // Classes //
  4.  
  5.     function Button(node, directory, animating, frames, anicallback, interval, callback) {
  6.         var self = this;
  7.         
  8.         // Events //
  9.             addEvent(node, "mousemove", false, function() {
  10.                 if (!self.hover) {
  11.                     self.hover = true;
  12.                     if (self.timer == null)
  13.                         self.update();
  14.                 }
  15.             });
  16.             
  17.             addEvent(node, "mousedown", false, function(event) {
  18.                 self.active = true;
  19.                 if (self.timer == null)
  20.                     self.update();
  21.                 buttons.current = self;
  22.                 event.returnValue = false;
  23.             });
  24.             
  25.             addEvent(node, "mouseout", false, function() {
  26.                 self.hover = false;
  27.                 if (self.timer == null)
  28.                     self.update();
  29.             });
  30.         
  31.         // User-set variables //
  32.         
  33.             this.node = node;
  34.             this.directory = directory;
  35.             this.animating = animating;
  36.             this.frames = frames;
  37.             this.interval = interval;
  38.             this.callback = callback;
  39.             this.anicallback = anicallback;
  40.         
  41.         // Variables //
  42.         
  43.             this.hover = false;
  44.             this.active = false;
  45.             this.frame = 1;
  46.             this.timer = null;
  47.     }
  48.     
  49.     Button.prototype.update = function() {
  50.         this.node.src = "media/" + this.directory + "/" + (this.hover ? (this.active ? "active" : "hover") : "normal") + (this.animating ? "/" + this.frame : "") + ".png";
  51.     }
  52.     
  53.     Button.prototype.start = function() {
  54.         if (this.animating && this.timer == null) {
  55.             var self = this;
  56.             this.timer = setInterval(function() {
  57.                 if (self.frame == self.frames)
  58.                     self.frame = 1;
  59.                 else
  60.                     self.frame += 1
  61.                 if (visible)
  62.                     self.update();
  63.                 self.anicallback();
  64.             }, this.interval);
  65.         }
  66.     }
  67.     
  68.     Button.prototype.stop = function() {
  69.         if (this.timer != null) {
  70.             clearInterval(this.timer);
  71.             this.timer = null;
  72.         }
  73.     }
  74.  
  75. // Events //
  76.  
  77.     addEvent(window, "mouseup", false, function() {
  78.         if (buttons.current != null) {
  79.             buttons.current.active = false;
  80.             if (buttons.current.hover) {
  81.                 buttons.current.hover = false;
  82.                 buttons.current.update();
  83.                 buttons.current.callback();
  84.             }
  85.             buttons.current = null;
  86.         }
  87.     });
  88.  
  89. // Functions //
  90.  
  91.     function addButton(node, directory, callback) {
  92.         buttons.instances[node.id] = new Button(node, directory, false, 0, null, 0, callback);
  93.     }
  94.     
  95.     function addAnimatingButton(node, directory, frames, anicallback, interval, callback) {
  96.         buttons.instances[node.id] = new Button(node, directory, true, frames, anicallback, interval, callback);
  97.     }
  98.  
  99.  
  100. // Variables //
  101.  
  102.     var buttons = {instances: new Array(), current: null};